import 'package:customer/widgets/gyy_button.dart';
import 'package:customer/widgets/gyy_dialog.dart';
import 'package:fluent_ui/fluent_ui.dart';

import '../../constant/edit_constant.dart';
import '../../db/entity/AttributeBean.dart';
import '../../main.dart';
import '../../utils/snackbar.dart';
import '../../widgets/gyy_theme.dart';

//属性编辑
class EditAttributePage extends StatelessWidget {

  final int id;
  final String name;
  final String code;
  EditAttributePage(this.id,this.name, this.code,  {super.key});

  var attributeDao = database.attributeDao;
  TextEditingController attributeNameController = TextEditingController();
  TextEditingController attributeCodeController = TextEditingController();


  //新增属性
  addOne(BuildContext context, String name, String code) async{
    debugPrint('属性：' + name + '  ' + code);
    AttributeBean? bean = await attributeDao.findById(id);
    if(bean != null) {
      bean.attributeName = name;
      await attributeDao.updateOne(bean);
    }
    Navigator.pop(context, DialogConstant.ok);
  }

  @override
  Widget build(BuildContext context) {

    attributeNameController.text = name;
    attributeCodeController.text = code;


    var theme = FluentTheme.of(context);
    bool checkInput() {
      if (attributeNameController.text.isEmpty) {
        showMessage(context, '请输入属性名称');
        return false;
      }
      if (attributeCodeController.text.isEmpty) {
        showMessage(context, '请输入属性编号');
        return false;
      }
      return true;
    }

    return SingleChildScrollView(
      child: Column(
        children: [
          sizeBoxV10,
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              spacer,
              Text(StringConstant.edit, style: theme.typography.title, ),
              spacer,
              IconButton(icon: const Icon(FluentIcons.chrome_close), onPressed: (){
                Navigator.pop(context, DialogConstant.cancel);
              })
            ],
          ),
          sizeBoxV10,
          sizeBoxV10,
          Row(
            children: [
              Expanded(
                  flex: 5, child: GyyTextInput('属性名称', attributeNameController)),
              sizeBoxH10,
              Expanded(
                  flex: 5,
                  child: GyyTextInput('属性code：', attributeCodeController, readOnly: true)),
            ],

          ),
          Container(
            constraints: BoxConstraints.tight(Size(10, 200)),
          ),
          BottomButton(StringConstant.save, (){
            if(checkInput()) {
              addOne(context, attributeNameController.text, attributeCodeController.text);
            }
          })
        ],
      ),
    );
  }
}
